import sys, os, warnings
from qt_core import *

from AppCore.SYS import *
from GUICore import *
from GUI import *


class MainWindow(QMainWindow):
    def __init__(self, **kargs):
        super().__init__()
        # 设置窗口接受鼠标事件
        self.setMouseTracking(True)

        # 加载配置
        self.settings = kargs["sys_settings"]
        self.config = kargs["sys_config"]
        for key, value in self.config.items():
            setattr(self, key, value)
        self.ColorPalette = ColorPalette()
        self.ColorPalette.update(self.themes[f'{self.settings["theme_name"]}'])
        # 加载UI
        self.ui = UI_MainWindow()
        self.ui.setup_ui(self)
        SetupMainWindow.setup_gui(self)
        # 渲染/展示
        self.show()

    def btn_clicked(self):
        # 左导航菜单
        # ///////////////////////////////////////////////////////////////
        btn = SetupMainWindow.setup_btns(self)
        if btn.objectName() != "btn_settings":
            self.ui.left_menu.deselect_all_tab()
        try:
            top_settings = MainFunctions.get_title_bar_btn(self, "btn_top_settings")
            top_settings.set_active(False)
        except AttributeError:
            pass
        if btn.objectName() == "btn_home":
            self.ui.left_menu.select_only_one(btn.objectName())
            MainFunctions.set_page(self, self.ui.load_pages.page_1)
            self.ui.title_bar.set_title(self.app_lang["btn_home"])
        if btn.objectName() == "btn_e2w":
            self.ui.left_menu.select_only_one(btn.objectName())
            MainFunctions.set_page(self, self.ui.load_pages.page_2)
            self.ui.title_bar.set_title(self.app_lang["btn_e2w"])
        if btn.objectName() == "btn_ioconfig":
            self.ui.left_menu.select_only_one(btn.objectName())
            MainFunctions.set_page(self, self.ui.load_pages.page_3)
            self.ui.title_bar.set_title(self.app_lang["btn_ioconfig"])
        if btn.objectName() == "btn_dp832":
            self.ui.left_menu.select_only_one(btn.objectName())
            MainFunctions.set_page(self, self.ui.load_pages.page_4)
            self.ui.title_bar.set_title(self.app_lang["btn_dp832"])
        if btn.objectName() == "btn_info":
            if not MainFunctions.left_column_is_visible(self):
                self.ui.left_menu.select_only_one_tab(btn.objectName())
                MainFunctions.toggle_left_column(self)
                self.ui.left_menu.select_only_one_tab(btn.objectName())
            else:
                if btn.objectName() == "btn_close_left_column":
                    self.ui.left_menu.deselect_all_tab()
                    MainFunctions.toggle_left_column(self)
                self.ui.left_menu.select_only_one_tab(btn.objectName())
            if btn.objectName() != "btn_close_left_column":
                MainFunctions.set_left_column_menu(
                    self,
                    menu=self.ui.left_column.menus.menu_2,
                    title=self.app_lang["left_column_info_title"],
                    icon_path=Functions.set_svg_icon("icon_info.svg"),
                )
        if (
            btn.objectName() == "btn_more"
            or btn.objectName() == "btn_close_left_column"
        ):
            if not MainFunctions.left_column_is_visible(self):
                MainFunctions.toggle_left_column(self)
                self.ui.left_menu.select_only_one_tab(btn.objectName())
            else:
                if btn.objectName() == "btn_close_left_column":
                    self.ui.left_menu.deselect_all_tab()
                    MainFunctions.toggle_left_column(self)
                self.ui.left_menu.select_only_one_tab(btn.objectName())
            if btn.objectName() != "btn_close_left_column":
                MainFunctions.set_left_column_menu(
                    self,
                    menu=self.ui.left_column.menus.menu_1,
                    title=self.app_lang["left_column_io_test_title"],
                    icon_path=Functions.set_svg_icon("icon_more.svg"),
                )
        # 标题栏菜单
        # ///////////////////////////////////////////////////////////////
        if btn.objectName() == "btn_top_settings":
            if not MainFunctions.right_column_is_visible(self):
                btn.set_active(True)
                MainFunctions.toggle_right_column(self)
            else:
                btn.set_active(False)
                MainFunctions.toggle_right_column(self)
            top_settings = MainFunctions.get_left_menu_btn(self, "btn_settings")
            top_settings.set_active_tab(False)
        if btn.objectName() == "select_path_bt":
            pass

    def btn_released(self):
        btn = SetupMainWindow.setup_btns(self)

    def resizeEvent(self, event):
        SetupMainWindow.resize_grips(self)

    def mousePressEvent(self, event):
        self.dragPos = event.globalPosition().toPoint()
        # 点击窗口其他区域时转移焦点
        self.focusNextChild()


if __name__ == "__main__":
    print(1)
    sys_settings = YamlHandler("res/SYS/settings.yml")
    for i in sys_settings["display"]:
        print(i)
    app = QApplication(sys.argv)
    app.setWindowIcon(QIcon(Functions.set_ico(sys_settings["logo"])))
    loading_window = LoadingWindow(
        Functions.set_svg_image(sys_settings["logo_loading"])
    )
    loading_window.loaded.connect(
        lambda: MainWindow(
            sys_settings=sys_settings,
            sys_config=loading_window.loader.sys_config,
        )
    )
    loading_window.show()
    sys.exit(app.exec())
